package com.example.smart.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.config.JwtHelper;
import com.example.smart.mapper.UserMapper;
import com.example.smart.pojo.User;
import com.example.smart.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JwtHelper jwtHelper;

    // 用户名正则达式
    private static final String USERNAME_REGEX = "^[a-zA-Z0-9_-]{4,16}$";
    // 邮箱正则达式
    private static final String EMAIL_REGEX = "\\w+@(\\w+\\.)+\\w+";
    // 手机号正则达式
    private static final String PHONE_REGEX = "^1[3456789]\\d{9}$";
    private Boolean isUserName(String str) {
        if (str == null) {
            return false;
        }
        return str.matches(USERNAME_REGEX);
    }

    public Boolean isPhoneNumber(String str) {
        if (str == null) {
            return false;
        }
        return str.matches(PHONE_REGEX);
    }

    private Boolean isEmail(String str) {
        if (str == null) {
            return false;
        }
        return str.matches(EMAIL_REGEX);
    }

    @Override
    public Map<String, Object> login(User users) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        String userName = users.getUserName();
        String userEmail = users.getUserEmail();
        String userPhone = users.getUserPhone();

        Boolean isUserName = isUserName(userName);
        Boolean isPhone = isPhoneNumber(userEmail);
        Boolean isEmail = isEmail(userPhone);
        //查询条件，通过用户名来查找用户
        if (isUserName) {
            queryWrapper.eq("user_name", userName);
        } else if (isEmail) {
            queryWrapper.eq("user_email", userEmail);
        } else if (isPhone) {
            queryWrapper.eq("user_phone", userPhone);
        }
        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            map.put("state", "nource");
        } else {
            //有用户，判断密码是否正确
            //密码比对就应该是前端传递的明文密码+盐最后MD5加密和数据库加密密码比对后再跟数据库密码比对
            if (user.getPassword().equals(SecureUtil.md5(users.getPassword() + user.getUserSalt()))) {
                String token;
                if (isUserName) {
                    token = jwtHelper.getToken(user.getUserId(), userName, "user");
                } else if (isEmail) {
                    token = jwtHelper.getToken(user.getUserId(), userEmail, "user");
                } else if (isPhone) {
                    token = jwtHelper.getToken(user.getUserId(), userPhone, "user");
                } else {
                    return map;
                }
                map.put("token", token);
                map.put("state", "success");
            } else {
                map.put("state", "error");
            }
        }
        return map;
    }
}
